package com.project.frba.bifront.server.rpc;

import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.project.frba.bi.BiUtil;
import com.project.frba.bi.descriptor.DescriptorsLoader;
import com.project.frba.bi.user.UserDAO;
import com.project.frba.bifront.client.model.User;
import com.project.frba.bifront.client.rpc.UserServices;

public class UserServicesImpl extends RemoteServiceServlet implements UserServices {
	
	public String login(String username, String password){
		User user = this.getUserByUsername(username);		
		
		if(user.getPassword().equals(password)){
			String sessionId = new Long(new Random().nextLong()).toString();
			if("admin".equals(user.getPrivilege())){
				sessionId = sessionId + "A";
			}
			HttpSession session = this.getThreadLocalRequest().getSession();
			session.setAttribute("sid", sessionId);
			session.setAttribute("userId", user.getId());
			session.setAttribute("privilege", user.getPrivilege());
			
			DescriptorsLoader.loadDescriptors();
			
			return sessionId;
		}
		
		return "0";
	}
	
	public Boolean logout(){
		HttpSession session = this.getThreadLocalRequest().getSession();
		session.setAttribute("sid", null);
		session.setAttribute("privilege", null);
		session.setAttribute("userId", null);
		return true;
	}
	
	public Boolean validateSessionId(String sessionId) {
		HttpServletRequest request = this.getThreadLocalRequest();
		if(request.getSession().getAttribute("sid") != null){
			String sid = request.getSession().getAttribute("sid").toString();
			if(sid.equals(sessionId)){
				return true;				
			}
		}
		return false;
	}
	

	public User getUserById(Long id) {
		ApplicationContext context 	= BiUtil.getApplicationContext();
		UserDAO userDao = (UserDAO)context.getBean("userDao");
		User user = userDao.getUserById(id);
		return user;
	}
	

	public User getUserByUsername(String username) {
		ApplicationContext context 	= BiUtil.getApplicationContext();
		UserDAO userDao = (UserDAO)context.getBean("userDao");
		User user = userDao.getUserByUsername(username);
		return user;
	}
	

	public Boolean removeUser(Long id) {
		ApplicationContext context 	= BiUtil.getApplicationContext();
		UserDAO userDao = (UserDAO)context.getBean("userDao");
		try{
			userDao.removeUser(id);			
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	
	public List<User> getAllUsers() {
		ApplicationContext context 	= BiUtil.getApplicationContext();
		UserDAO userDao = (UserDAO)context.getBean("userDao");
		return userDao.getAllUsers();
	}
	
	
	public Long storeUser(User user) {
		ApplicationContext context 	= BiUtil.getApplicationContext();
		UserDAO userDao = (UserDAO)context.getBean("userDao");
		
		Long id = user.getId() ;
		try {
			if(user.getId() == 0L){
				Integer idInt = userDao.insertUser(user.getUsername(), user.getPassword(), getPrivilegeId(user.getPrivilege()));
				id = new Long(idInt);
			} else {
				userDao.updateUser(user.getId(), user.getUsername(), user.getPassword(), getPrivilegeId(user.getPrivilege()));
			}
		} catch (Exception e) {
			return 0L;
		}
		
		
		return id;
	}
	
	public Long getPrivilegeId(String name){
		if("admin".equals(name)){
			return 1L;
		}
		if("general".equals(name)){
			return 2L;
		}
		
		return 3L;
	}
	

}
